from flask import jsonify, request, make_response
import xml.etree.ElementTree as ET
import dicttoxml

class Response:

    @staticmethod
    def make_response_json(data=None, status=200, message=None):
        response = {
            'data': data,
            'message': message,
            'status': status
        }
        print(response)
        return jsonify(response)

    @staticmethod
    def make_response_xml(data=None, status=200, message=None):
        response = {
            'data': data,
            'message': message,
            'status': status
        }
        xml_str = dicttoxml.dicttoxml(response, custom_root='response', attr_type=False).decode('utf-8')
        response = make_response(xml_str)
        response.headers['Content-Type'] = 'application/xml'
        return response

    @staticmethod
    def make_response(data=None, status=200, message=None):
        # 检查请求的Accept头部
        accept_header = request.headers.get('Accept', '')
        # 根据Accept头部决定响应格式
        if 'application/xml' in accept_header or 'text/xml' in accept_header:
            return Response.make_response_xml(data, status, message)
        else:
            return Response.make_response_json(data, status, message)
